package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.Person;
import com.gljx.web.Entity.Team;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.PersonService;
import com.gljx.web.service.TeamService;

@Controller
@RequestMapping("/")
public class TeamController {

	//自动注入
	@Autowired
	private TeamService teamService;
	
	//自动注入
	@Autowired
	private CustomLogService  customLogService;
	
	//自动注入
	@Autowired
	private PersonService personService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	/**
	 * 查询教练队
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryTeam/look.action")
	public String queryTeam(HttpServletRequest request, String flag) {
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//根据公司id查询
		List<Team> teams = teamService.queryTeamByFcontrolunitid(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("teams", teams);
		request.setAttribute("flag", flag);
		return "/backHome/personData/team/teamList";
	}
	
	/**
	 * 根据名称查询教练队
	 * @param request
	 * @param fnameL2 名称
	 * @return
	 */
	@RequestMapping("/queryTeamByFnameL2")
	public String queryTeamByFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		if("".equals(fnameL2) || fnameL2 == null){
			queryTeam(request, null);//如果为空就调用查询的方法
		}else{
            //去除空格
            fnameL2 = fnameL2.trim();
            // 查询数据
			List<Team> teams = teamService.queryTeamByFcontrolunitidAndFnameL2(user.getFcompanyid(), fnameL2);
			//把数据传到页面
			request.setAttribute("teams", teams);
			request.setAttribute("fnameL2", fnameL2);
		}
		return "/backHome/personData/team/teamList";
	}
	
	@RequestMapping("/insertTeam")
	public String insertTeam(HttpServletRequest request,String type){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//获取最大最大编码
        /*String fnumber =  customLogService.selectFunmber("ct_ds_team",user.getFcompanyid());*/
		//获取下一个编码
        String number = GetFnumber.fnumbers("DC");
		//拼接SQL语句
    	StringBuffer psql = new StringBuffer();
    	psql.append("select T.FID, T.FNUMBER,to_char(t.CFBIRTHDAY,'YYYY-MM-DD ') cfbirthdays,to_char(t.cfstartdate,'YYYY-MM-DD ') cfstartdate,");
		psql.append("to_char(t.cfenddate,'YYYY-MM-DD ') cfenddate,to_char(t.cfbegindate,'YYYY-MM-DD ') cfbegindate,to_char(t.CFHTDQSJ,'YYYY-MM-DD ') cfhtdqsj,");
		psql.append("T.CFIDCARDNO, T.CFCELL, T.CFADDRESS,T.CFPERSONTYPE, T.CFSEX,T.CFXCTZ, T.CFGWYD, T.CFQJJL, T.CFWJJLJL,p.fname_l2 as positiongradeName,t.cfaccountsno, ");
		psql.append("T.CFSHJLJL,T.FNAME_L2,T.CFZSTC,T.CFPXTC,A.FNAME_L2 AS CFPERSONDEPNAME,C.fnumber AS CFCARNAME from ct_ds_person t left join ");
		psql.append("CT_DS_ADMIN A ON A.FID=T.CFPERSONDEPID left join CT_DS_CAR C ON C.FID=T.CFCARID left join CT_DS_POSITIONGRADE p on t.cfjobgradeid=p.fid ");
        psql.append(" where t.cfficompanyid = '" + user.getFcompanyid() + "'");
        //查询职员
		List<Person> persons = personService.selectPerson(psql.toString());
		//查询组织
		Department department = departmentService.queryAdminByFid(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("fnumber", number);
		request.setAttribute("department", department);
		request.setAttribute("persons", persons);
		request.setAttribute("type", type);
		return "/backHome/personData/team/editTeam";
	}
	
	/**
	 * 根据名称查询职员
	 * @param name
	 * @return
	 */
	@RequestMapping("/quePersonByName")
	@ResponseBody
    public List<Person> quePersonByName(String name, HttpServletRequest request) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //拼接SQL语句
    	StringBuffer psql = new StringBuffer();
    	psql.append("select T.FID, T.FNUMBER,to_char(t.CFBIRTHDAY,'YYYY-MM-DD ') cfbirthdays,to_char(t.cfstartdate,'YYYY-MM-DD ') cfstartdate,");
		psql.append("to_char(t.cfenddate,'YYYY-MM-DD ') cfenddate,to_char(t.cfbegindate,'YYYY-MM-DD ') cfbegindate,to_char(t.CFHTDQSJ,'YYYY-MM-DD ') cfhtdqsj,");
		psql.append("T.CFIDCARDNO, T.CFCELL, T.CFADDRESS,T.CFPERSONTYPE, T.CFSEX,T.CFXCTZ, T.CFGWYD, T.CFQJJL, T.CFWJJLJL,p.fname_l2 as positiongradeName,t.cfaccountsno, ");
		psql.append("T.CFSHJLJL,T.FNAME_L2,T.CFZSTC,T.CFPXTC,A.FNAME_L2 AS CFPERSONDEPNAME,C.fnumber AS CFCARNAME from ct_ds_person t left join ");
		psql.append("CT_DS_ADMIN A ON A.FID=T.CFPERSONDEPID left join CT_DS_CAR C ON C.FID=T.CFCARID left join CT_DS_POSITIONGRADE p on t.cfjobgradeid=p.fid ");
        psql.append(" where t.cfficompanyid = '" + user.getFcompanyid() + "'");
        if(name == null || "".equals(name)){
			
		}else{
            psql.append("and t.FNAME_L2 like '%" + name + "%'");
        }
		//查询职员
		List<Person> persons = personService.selectPerson(psql.toString());
		return persons;
	}
	
	/**
	 * 根据fid查询
	 * @param fid
	 * @return
	 */
	@RequestMapping("/quePersonByFid")
	@ResponseBody
	public Person quePersonByFid(String fid){
		Person person = personService.queryPersonByFid(fid);
		return person;
	}
	
	/**
	 * 添加教练队
	 * @param request
	 * @param team
	 * @return
	 */
	@RequestMapping("/addTeam")
	public String addTeam(HttpServletRequest request,Team team){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//利用UUID生成fid
		team.setFid(GetFid.UUIDIntoFid());
		//控制单元id
		team.setFcontrolunitid(user.getFcompanyid());
		//创建人id
		team.setFcreatorid(user.getFid());
		//公司id
		team.setCfficompanyid(user.getFcompanyid());
		//获取当前时间
		String createTime = TimeFormat.timeFormat(new Date());
		team.setFcreatetime(createTime);
		//把属性添加进去
		teamService.addTeam(team);
		//添加日志
		customLogService.addLog("职员基础资料", createTime+"对教练队进行添加操作", null, user.getFid());
		//重定向到添加页面
		return "redirect:/DrivingSys/insertTeam?type=3";
	}
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param fid 一个
	 * @param deleteId 多个
	 * @return
	 */
	@RequestMapping("/deleteTeamsByFid")
	public String deleteTeamsByFid(HttpServletRequest request,String fid,String deleteId[]){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				teamService.deleteTeamByFid(deleteId[i]);
			}
		}else{
			teamService.deleteTeamByFid(fid);
		}
		//添加日志
		customLogService.addLog("职员基础资料", TimeFormat.timeFormat(new Date())+"对教练队进行添加操作", null, user.getFid());
		//调用查询的方法
		queryTeam(request, null);
		return "/backHome/personData/team/teamList";
	}
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid
	 * @return
	 */
	@RequestMapping("/selectTeanmByFid")
	public String selectTeanmByFid(HttpServletRequest request,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//查询组织
		Department department = departmentService.queryAdminByFid(user.getFcompanyid());
		//查询数据
		Team team = teamService.queryTeamByFid(fid);
		//拼接SQL语句
    	StringBuffer psql = new StringBuffer();
    	psql.append("select T.FID, T.FNUMBER,to_char(t.CFBIRTHDAY,'YYYY-MM-DD ') cfbirthdays,to_char(t.cfstartdate,'YYYY-MM-DD ') cfstartdate,");
		psql.append("to_char(t.cfenddate,'YYYY-MM-DD ') cfenddate,to_char(t.cfbegindate,'YYYY-MM-DD ') cfbegindate,to_char(t.CFHTDQSJ,'YYYY-MM-DD ') cfhtdqsj,");
		psql.append("T.CFIDCARDNO, T.CFCELL, T.CFADDRESS,T.CFPERSONTYPE, T.CFSEX,T.CFXCTZ, T.CFGWYD, T.CFQJJL, T.CFWJJLJL,p.fname_l2 as positiongradeName,t.cfaccountsno, ");
		psql.append("T.CFSHJLJL,T.FNAME_L2,T.CFZSTC,T.CFPXTC,A.FNAME_L2 AS CFPERSONDEPNAME,C.fnumber AS CFCARNAME from ct_ds_person t left join ");
		psql.append("CT_DS_ADMIN A ON A.FID=T.CFPERSONDEPID left join CT_DS_CAR C ON C.FID=T.CFCARID left join CT_DS_POSITIONGRADE p on t.cfjobgradeid=p.fid ");
		//查询职员
		List<Person> persons = personService.selectPerson(psql.toString());
		//把数据传到页面
		request.setAttribute("department", department);
		request.setAttribute("team", team);
		request.setAttribute("persons", persons);
		return "/backHome/personData/team/editTeam";
	}
	
	/**
	 * 修改
	 * @param request
	 * @param team 属性自动封装
	 * @return
	 */
	@RequestMapping("/modifyTeam")
	public String modifyTeam(HttpServletRequest request,Team team){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//控制单元id
		team.setFcontrolunitid(user.getFcompanyid());
		//创建人id
		team.setFlastupdateuserid(user.getFid());
		//公司id
		team.setCfficompanyid(user.getFcompanyid());
		//获取当前时间
		String createTime = TimeFormat.timeFormat(new Date());
		team.setFlastupdatetime(createTime);
		//修改
		teamService.modifyTeam(team);
		//调用查询的方法
		return commonController.skipUrl("/queryTeam/look.action");
	}

    /**
     * 导出Execl
     *
     * @param response
     * @param request
     * @param fnameL2
     */
    @RequestMapping("/importTeam")
    public void importTeam(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //定义接收类型
        List<Object> data = new ArrayList<>();
        //查询数据
        List<Team> teams = teamService.queryTeamByFcontrolunitidAndFnameL2(user.getFcompanyid(), fnameL2.trim());
        for (Team team : teams) {
            data.add(team.getFnumber());
            data.add(team.getFnameL2());
            data.add(team.getFsimplename());
            data.add(team.getFdescriptionL2());
            data.add(team.getFcreatetime());
            data.add(team.getFlastupdatetime());
            data.add(team.getCreateName());
            data.add(team.getUpdateName());
            data.add(stateToString.subject(team.getCfkemu()));
            data.add(team.getTeamName());
        }
        //头部数据
        String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间", "创建者", "修改人", "科目", "队长"};
        //长度
        Integer length[] = {6500, 3000, 3000, 3000, 5000, 5000, 3000, 3000, 2600, 3000};
        try {
            //导出Excel
            CommonExport.importExcel(response, "教练队" + TimeFormat.getNowTime2() + ".xls", "教练队", data, head, length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
